//
//  DropDownView.m
//  JiuLe
//
//  Created by xcq on 15/12/2.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "DropDownView.h"
#import "CLMarcoDefine.h"
#import "UIColor+RGBConverHex.h"
#import "JLDropDownTableViewCell.h"
@implementation DropDownView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self viewDidLoad];
    }
    return self;
}

- (void)viewDidLoad {
    tableView = [[UITableView alloc] initWithFrame:(CGRect){ {0,10},{ self.bounds.size.width
        , self.bounds.size.height}} style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.dataSource = self;
    tableView.rowHeight = 32;
    tableView.scrollEnabled = NO;
    tableView.layer.cornerRadius = 5;
    tableView.layer.borderWidth = 1;
    tableView.layer.borderColor = [[UIColor grayColor] CGColor];
    tableView.layer.masksToBounds = YES;
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    [self addSubview:tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JLDropDownTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"JLDropDownTableViewCell" owner:self options:nil] lastObject];
    NSDictionary *model = _dataSource[indexPath.row];
    cell.title.text = [model objectForKey:@"name"];
    cell.title.font = iOS8Font(14.f);
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < _dataSource.count) {
        NSDictionary *model = _dataSource[indexPath.row];
        NSString *type = [NSString stringWithFormat:@"%d",[[model objectForKey:@"type"] intValue]];
        _action(indexPath.row , type);
    }
}

- (void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    [tableView reloadData];
}

@end
